import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export const store = () => new Vuex.Store({
     state:{
         username:'',         //账号
         userKey:'',
         money:0,       //余额
     },
     getters:{
         username:state => {
             return state.username || localStorage.getItem('username')
          },
         userkey:state => {
             return state.userKey || localStorage.getItem('userkey')
          },
          money: state => {
             //因为||运算符是遇到真就停止，我在state里面定义的money是0，逻辑运算符判断就是false，所以要把其放在后面
             return localStorage.getItem('money') || state.money
        }

     },
     mutations:{
          setUsername: (state,username) => {
              state.username = username
              localStorage.setItem('username',username)
          },
          setUserkey:(state,userkey) => {
              state.userKey = userkey
              localStorage.setItem('userKey',userkey)
          },
          setMoney:(state,money) => {
              state.money = money
              localStorage.setItem('money',money)
          }
     }
})
